<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>

<%@ taglib uri="/WEB-INF/hexacta.tld" prefix="hexacta"%>

<%@ page import="java.util.Date"%>

<head>
<sx:head cache="true" />
<link rel="stylesheet" href="/hrs-intranet/styles/displaytag.css"
	type="text/css" />
</head>

<link type="text/css" href="/hrs-intranet/js/jquery/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/hrs-intranet/js/jquery/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="/hrs-intranet/js/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>

<script type="text/javascript" src="/hrs-intranet/js/cluetip/jquery.cluetip.js"></script>
<link type="text/css" href="/hrs-intranet/js/cluetip/jquery.cluetip.css" rel="stylesheet" />
	
<link rel="stylesheet" href="/hrs-intranet/styles/button.css" type="text/css" />

<script type="text/javascript">
	function confirmRow(id){
		var row = id.parentNode.parentNode;
		var estimated = row.cells[4].firstChild;
		estimated.disabled = true;
		var user = row.cells[7].firstChild;
		var userName = document.getElementById("loggedUser").value;
		var userId = document.getElementById("loggedUserId").value;
		user.value = userId;
		user.textContent = userName;
		var date = row.cells[8].firstChild;
		date.value = new Date();
		date.textContent = date.value.getDate()+'/'+date.value.getMonth()+'/'+date.value.getFullYear()+'  '+date.value.getHours()+':'+date.value.getMinutes()+':'+date.value.getSeconds() ;
		date.value = date.value.getTime();
		var link = row.cells[9].firstChild;
		link.textContent = "Confirmed";
		link.onclick = "";
	}
	
	function saveRow(id){
		var toSave = "";
		
		var row = id.parentNode.parentNode;
		
		var id = row.id;
		var earn = row.cells[2].firstChild.value;
		var bill= row.cells[3].firstChild.value;
		var estimated = row.cells[4].firstChild.checked;
		var comment = row.cells[5].firstChild.value;
		var user = row.cells[7].firstChild.id;
		var date = row.cells[8].firstChild.id;
		
		var link = row.cells[9].firstChild;
		if(link != null){
			link.hidden = true;
		}
		
		row.cells[2].firstChild.disabled = true;
		row.cells[3].firstChild.disabled = true;
		row.cells[4].firstChild.disabled = true;
		row.cells[5].firstChild.disabled = true;
		
		
		row.cells[10].firstChild.value = "Saved";
		row.cells[10].firstChild.disabled = true;
		
		toSave = id+','+earn+','+bill+','+estimated+','+comment+','+user+','+date+';' 
		
		var accrueds = document.getElementById("editedAccrueds").value; 
		document.getElementById("editedAccrueds").value = accrueds + toSave;
	}

	function checkEstimated(id){
		var row = id.parentNode.parentNode;
		var bill = row.cells[3].firstChild;
		var confirm = row.cells[9].firstChild;
		if (id.checked == true){
			bill.value = "0";
			bill.disabled = true;
			if(confirm != null){
				confirm.hidden = true;
			}
		}else{
			bill.disabled = false;
			if(confirm != null){
				confirm.hidden = false;
			}	
		}
	}

	
</script>

<s:form cssClass="formClass">

		<s:hidden name="editedAccrueds" id="editedAccrueds"/>
		<s:hidden name="loggedUser" id="loggedUser"/>
		<s:hidden name="loggedUserId" id="loggedUserId"/>
	
			<s:select list="partnerList" listValue="person.name" listKey="id"
				key="selectedPartnerName" name="selectedPartnerName"
				keyValue="selectedPartnerId" id="partnerSelected"
				label="Referent Partners" showDownArrow="false"
				cssStyle="width: 100%" />

			<s:select label="Month" headerKey="1" key="selectedMonth"
				name="selectedMonth"
				list="#{'1':'January', '2':'February', '3':'March', '4':'April', '5':'May', '6':'June', '7':'July', '8':'August', '9':'September', '10':'October', '11':'November', '12':'December'}"
				value="selectedMonth" />

			<s:textfield name="selectedYear" key="selectedYear" label="Year"
				maxlength="4" />
			
			<table>
				<s:submit key="Search" action="ReferringAction_filterReferrings" cssClass="btn" style="text-align: center;"/>
			</table>
	<s:if test="accruedsList != null">
	<table id="partnerAccrued" class="listable" cellpadding="0" cellspacing="0" style="text-align: center; vertical-align: middle;">
		<thead>
			<tr>
				<td>Project</td>
				<td>Currency</td>
				<td>Earn</td>
				<td>Bill</td>
				<td>Estimated</td>
				<td>Comment</td>
				<td>Reference</td>
				<td>User</td>
				<td>Date</td>
				<td align="center" colspan="2">Action</td>
			</tr>
		</thead>
		<tbody id="partnerAccruedBody">
			<s:iterator id="accruedsList" value="accruedsList">
				<tr class="even" id="<s:property value="accrued.id"/>">
					<td align="center" valign="middle"><label><s:property value="project"/></label></td>
					<td><label><s:property value="currency"/></label></td>
					<td><input type="text" value="<s:property value="accrued.earn"/>"></td>
					<s:if test='%{accrued.estimated}'>
						<td><input type="text" value="<s:property value="accrued.bill"/>" disabled="disabled"></td>
					</s:if>
					<s:else>
						<td><input type="text" value="<s:property value="accrued.bill"/>"></td>
					</s:else>
					<s:if test='%{accrued.estimated}'>
						<td><input  type="checkbox" value="<s:property value="accrued.estimated"/>" checked="checked" onclick="javascript: checkEstimated(this);">
						</td>
					</s:if>
					<s:else>
						<td><input type="checkbox" value="<s:property value="accrued.estimated"/>" onclick="javascript: checkEstimated(this);">
						</td>
					</s:else>
					<td><input type="text" value="<s:property value="accrued.comment"/>"></td>
					<td><label></label></td>
					<td><label id="<s:property value="accrued.confirmation.user.person.user.id"/>"><s:property value="accrued.confirmation.user.person.user.userName"/></label></td>
					<td><label id="<s:property value="accrued.confirmation.date.timeInMillis"/>" ><s:property value="accrued.confirmation.formatedDate"/></label></td>
					<s:if test='accrued.confirmation == null'>
						<td valign="middle"><s:a type="button" value="Confirm" class="Add" onclick="javascript: confirmRow(this);" cssStyle="">Confirm</s:a></td>
					</s:if>
					<s:else><td></td></s:else>
					<td><input type="button" value="Save" class="btn" width="30%" onclick="javascript: saveRow(this);" style="text-align: center"></td>
				</tr>
			</s:iterator>
		</tbody>
	</table>
	
	<s:if test="accruedsList.size > 0">
	<table width="100%">
		<tr>
			<td width="50%" align="right"><table>
					<s:submit action="ReferringAction_updateAccrueds" key="Ok" cssClass="btn" id="okbtn" style="text-align: center"/>
				</table></td>
			<td width="50%" align="left"><table> 
				<s:submit action="ReferringAction_filterReferrings" key="Cancel" cssClass="btn" id="okbtn" style="text-align: center" />
				</table></td>
		</tr>
	</table>
	</s:if>
	</s:if>
</s:form>